3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 class ObjectFactoryTest
extends PHPUnit_Framework_TestCase
{
23 use MediaWikiCoversValidator
;
26 * @covers ObjectFactory::getObjectFromSpec
28 public function testClosureExpansionDisabled() {
29 $obj = ObjectFactory
::getObjectFromSpec( [
30 'class' => 'ObjectFactoryTestFixture',
38 'setter' => [ function () {
42 'closure_expansion' => false,
44 $this->assertInstanceOf( 'Closure', $obj->args
[0] );
45 $this->assertSame( 'wrapped', $obj->args
[0]() );
46 $this->assertSame( 'unwrapped', $obj->args
[1] );
47 $this->assertInstanceOf( 'Closure', $obj->setterArgs
[0] );
48 $this->assertSame( 'wrapped', $obj->setterArgs
[0]() );
52 * @covers ObjectFactory::getObjectFromSpec
53 * @covers ObjectFactory::expandClosures
55 public function testClosureExpansionEnabled() {
56 $obj = ObjectFactory
::getObjectFromSpec( [
57 'class' => 'ObjectFactoryTestFixture',
65 'setter' => [ function () {
69 'closure_expansion' => true,
71 $this->assertInternalType( 'string', $obj->args
[0] );
72 $this->assertSame( 'wrapped', $obj->args
[0] );
73 $this->assertSame( 'unwrapped', $obj->args
[1] );
74 $this->assertInternalType( 'string', $obj->setterArgs
[0] );
75 $this->assertSame( 'wrapped', $obj->setterArgs
[0] );
77 $obj = ObjectFactory
::getObjectFromSpec( [
78 'class' => 'ObjectFactoryTestFixture',
79 'args' => [ function () {
83 'setter' => [ function () {
88 $this->assertInternalType( 'string', $obj->args
[0] );
89 $this->assertSame( 'unwrapped', $obj->args
[0] );
90 $this->assertInternalType( 'string', $obj->setterArgs
[0] );
91 $this->assertSame( 'unwrapped', $obj->setterArgs
[0] );
95 * @covers ObjectFactory::getObjectFromSpec
97 public function testGetObjectFromFactory() {
99 $obj = ObjectFactory
::getObjectFromSpec( [
100 'factory' => function ( $a, $b ) {
101 return new ObjectFactoryTestFixture( $a, $b );
105 $this->assertSame( $args, $obj->args
);
109 * @covers ObjectFactory::getObjectFromSpec
110 * @expectedException InvalidArgumentException
112 public function testGetObjectFromInvalid() {
113 $args = [ 'a', 'b' ];
114 $obj = ObjectFactory
::getObjectFromSpec( [
115 // Missing 'class' or 'factory'
121 * @covers ObjectFactory::getObjectFromSpec
122 * @dataProvider provideConstructClassInstance
124 public function testGetObjectFromClass( $args ) {
125 $obj = ObjectFactory
::getObjectFromSpec( [
126 'class' => 'ObjectFactoryTestFixture',
129 $this->assertSame( $args, $obj->args
);
133 * @covers ObjectFactory::constructClassInstance
134 * @dataProvider provideConstructClassInstance
136 public function testConstructClassInstance( $args ) {
137 $obj = ObjectFactory
::constructClassInstance(
138 'ObjectFactoryTestFixture', $args
140 $this->assertSame( $args, $obj->args
);
143 public static function provideConstructClassInstance() {
144 // These args go to 11. I thought about making 10 one louder, but 11!
147 '1 args' => [ [ 1, ] ],
148 '2 args' => [ [ 1, 2, ] ],
149 '3 args' => [ [ 1, 2, 3, ] ],
150 '4 args' => [ [ 1, 2, 3, 4, ] ],
151 '5 args' => [ [ 1, 2, 3, 4, 5, ] ],
152 '6 args' => [ [ 1, 2, 3, 4, 5, 6, ] ],
153 '7 args' => [ [ 1, 2, 3, 4, 5, 6, 7, ] ],
154 '8 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, ] ],
155 '9 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, ] ],
156 '10 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ] ],
157 '11 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ] ],
162 * @covers ObjectFactory::constructClassInstance
163 * @expectedException InvalidArgumentException
165 public function testNamedArgs() {
166 $args = [ 'foo' => 1, 'bar' => 2, 'baz' => 3 ];
167 $obj = ObjectFactory
::constructClassInstance(
168 'ObjectFactoryTestFixture', $args
173 class ObjectFactoryTestFixture
{
176 public function __construct( /*...*/ ) {
177 $this->args
= func_get_args();
179 public function setter( /*...*/ ) {
180 $this->setterArgs
= func_get_args();